package com.umeng.soexample.mvp.view.fragment;


import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.umeng.soexample.R;
import com.umeng.soexample.bean.CollectionListEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.mvp.view.adapter.CollectionListPTAdapter;
import com.umeng.soexample.mvp.view.adapter.CreateOrderViewPagerAdapter;
import com.umeng.soexample.mvp.view.custom.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectionListFragment extends Fragment implements View.OnClickListener {

    private View view;
    private List<CollectionListEntity> list = new ArrayList<>();
    private RadioGroup rg_collection_list;
    private NoScrollViewPager nsvp_collection_list;
    private CreateOrderViewPagerAdapter adapter;
    private List<View> views = new ArrayList<>();
    //TODO 普通商品
    private LinearLayout ll_collection_list_pt;
    private RecyclerView rv_collection_list_pt;
    private CollectionListPTAdapter ptAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_collection_list, container, false);
        initData();
        initView(view);
        initListener();
        return view;
    }

    /**
     * 数据库获取数据
     */
    private void initData() {
        Cursor cursor = USerService.getInstance().query("collection", null, null);
        while (cursor.moveToNext()) {
            CollectionListEntity entity = new CollectionListEntity();
            entity.goodsid = cursor.getString(cursor.getColumnIndex("goodsid"));
            entity.goods_img = cursor.getString(cursor.getColumnIndex("goods_img"));
            entity.goods_name = cursor.getString(cursor.getColumnIndex("goods_name"));
            entity.goods_price = cursor.getString(cursor.getColumnIndex("goods_price"));
            list.add(entity);
        }

    }

    /**
     * 添加监听
     */
    private void initListener() {
        //顶部商品类型选择
        rg_collection_list.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_collection_list_pt_goods:
                        nsvp_collection_list.setCurrentItem(0);
                        break;
                    case R.id.rb_collection_list_ufo_goods:
                        nsvp_collection_list.setCurrentItem(1);
                        break;
                    case R.id.rb_collection_list_jw_goods:
                        nsvp_collection_list.setCurrentItem(2);
                        break;
                }
            }
        });
        rg_collection_list.check(R.id.rb_collection_list_pt_goods);
        view.findViewById(R.id.iv_collection_list_back).setOnClickListener(this);//退出键
    }

    /**
     * 初始化控件赋值
     *
     * @param view
     */
    private void initView(View view) {
        rg_collection_list = (RadioGroup) view.findViewById(R.id.rg_collection_list);
        nsvp_collection_list = (NoScrollViewPager) view.findViewById(R.id.nsvp_collection_list);
        //TODO 普通商品
        View view_pt_goods = View.inflate(getContext(), R.layout.item_collection_list_pt_jw, null);
        ll_collection_list_pt = view_pt_goods.findViewById(R.id.ll_collection_list_pt_jw);
        rv_collection_list_pt = view_pt_goods.findViewById(R.id.rv_collection_list_pt_jw);
        if (list.size() == 0){
            ll_collection_list_pt.setVisibility(View.VISIBLE);
            rv_collection_list_pt.setVisibility(View.GONE);
        }else{
            ll_collection_list_pt.setVisibility(View.GONE);
            rv_collection_list_pt.setVisibility(View.VISIBLE);
            ptAdapter = new CollectionListPTAdapter(R.layout.item_collection_list_pt);
            ptAdapter.setNewData(list);
            rv_collection_list_pt.setLayoutManager(new LinearLayoutManager(getContext()));
            rv_collection_list_pt.setAdapter(ptAdapter);
        }
        View view_ufo_goods = View.inflate(getContext(), R.layout.item_collection_list_ufo, null);
        View view_jw_goods = View.inflate(getContext(), R.layout.item_collection_list_pt_jw, null);
        views.add(view_pt_goods);
        views.add(view_ufo_goods);
        views.add(view_jw_goods);
        adapter = new CreateOrderViewPagerAdapter(views);
        nsvp_collection_list.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_collection_list_back://退出键
                getActivity().finish();
                break;
        }
    }
}
